import java.util.Scanner;

public class BankerAlgorithmTest {

    public static void main(String[] args) {
        int number, resourceKindCount;

        Scanner scanner = new Scanner(System.in);
        System.out.println("Input the type of resource and number of customer:");
        resourceKindCount = scanner.nextInt();
        number = scanner.nextInt();
        scanner.nextLine();
        System.out.println("Input the amount of resource (maximum , allocated) of each customer:");
        int[][] max = new int[number][resourceKindCount];
        String[] name = new String[number];
        int[][] allocation = new int[number][resourceKindCount];
        int[] request = new int[resourceKindCount];
        String requestName = new String();
        int[][] need = new int[number][resourceKindCount];
        int[][] add = new int[number][resourceKindCount];
        int[] available = new int[resourceKindCount];
        String[] state = new String[number];
        int[][] work = new int[number][resourceKindCount];
        for (int i = 0; i < number; i++) {
            state[i] = "F";
        }
        for (int i = 0; i < number; i++) {
            name[i] = scanner.next();
            for (int j = 0; j < resourceKindCount; j++) {
                max[i][j] = scanner.nextInt();
            }

            for (int k = 0; k < resourceKindCount; k++) {
                allocation[i][k] = scanner.nextInt();
            }
        }
        for (int i = 0; i < number; i++) {
            for (int j = 0; j < resourceKindCount; j++) {
                need[i][j] = max[i][j] - allocation[i][j];
            }
        }
        System.out.println("Input  the amout of  resources(available):");
        for (int i = 0; i < resourceKindCount; i++) {
            available[i] = scanner.nextInt();
        }
        while (true) {
            System.out.println("1、judge the system security\n" +
                    "2、judge the request security\n" +
                    "3、quit");
            int a;
            a = scanner.nextInt();
            switch (a) {
                case 1: {
                    String[] name2 = new String[number];
                    int[][] allocation2 = new int[number][resourceKindCount];

                    int[][] need2 = new int[number][resourceKindCount];
                    int[][] add2 = new int[number][resourceKindCount];
                    int[] available2 = new int[resourceKindCount];
                    String[] state2 = new String[number];
                    int[][] work2 = new int[number][resourceKindCount];
                    for (int i = 0; i < number; i++) {
                        name2[i] = name[i];
                        state2[i] = state[i];
                        for (int j = 0; j < resourceKindCount; j++) {
                            available2[j] = available[j];
                        }
                        for (int j = 0; j < resourceKindCount; j++) {
                            need2[i][j] = need[i][j];
                        }
                        for (int j = 0; j < resourceKindCount; j++) {
                            add2[i][j] = add[i][j];
                        }
                        for (int j = 0; j < resourceKindCount; j++) {
                            allocation2[i][j] = allocation[i][j];
                        }
                        for (int j = 0; j < resourceKindCount; j++) {
                            work2[i][j] = work[i][j];
                        }
                    }
                    System.out.println("Name   Work      Need    Allocation   Work+ Allocation  Finish");
                    int b = 0;
                    int d = 0;
                    while (b != number) {
                        for (int i = 0; i < number; i++) {
                            int c = 0;
                            if (!state2[i].equals("T")) {
                                for (int j = 0; j < resourceKindCount; j++) {
                                    if (need2[i][j] <= available2[j]) {
                                        //  work[i][j] = available[j];
                                        // add[i][j] = work[i][j] + allocation[i][j];
                                        //  available[j] = add[i][j];
                                        c++;
                                    } else continue;

                                }
                            }
                            if (c == resourceKindCount) {
                                state2[i] = "T";
                                for (int j = 0; j < resourceKindCount; j++) {
                                    work2[i][j] = available2[j];
                                    add2[i][j] = work2[i][j] + allocation2[i][j];
                                    available2[j] = add2[i][j];
                                }
                                System.out.print(name2[i] + "    ");
                                for (int A = 0; A < resourceKindCount; A++) {
                                    System.out.print(work2[i][A] + " ");

                                }
                                System.out.print("   ");
                                for (int y = 0; y < resourceKindCount; y++) {
                                    System.out.print(need2[i][y] + " ");

                                }
                                System.out.print("    ");
                                for (int z = 0; z < resourceKindCount; z++) {
                                    System.out.print(allocation2[i][z] + " ");
                                }
                                System.out.print("        ");
                                for (int x1 = 0; x1 < resourceKindCount; x1++) {
                                    System.out.print(add2[i][x1] + " ");
                                }
                                System.out.print("            ");
                                System.out.println(state2[i]);
                                b++;
                                break;
                            }
                        }
                        d++;
                        if (d > number) {
                            System.out.println("Dangerous");
                            break;
                        }
                    }
                    if (d <= number)
                        System.out.println("SYSTEM SECURITY!!!");
                    break;
                }
                case 2: {
                    int h = 0;
                    String[] name1 = new String[number];
                    int[][] allocation1 = new int[number][resourceKindCount];
                    int[][] need1 = new int[number][resourceKindCount];
                    int[][] add1 = new int[number][resourceKindCount];
                    int[] available1 = new int[resourceKindCount];
                    String[] state1 = new String[number];
                    int[][] work1 = new int[number][resourceKindCount];
                    for (int i = 0; i < number; i++) {
                        name1[i] = name[i];
                        state1[i] = state[i];
                        for (int j = 0; j < resourceKindCount; j++) {
                            available1[j] = available[j];
                        }
                        for (int j = 0; j < resourceKindCount; j++) {
                            need1[i][j] = need[i][j];
                        }
                        for (int j = 0; j < resourceKindCount; j++) {
                            add1[i][j] = add[i][j];
                        }
                        for (int j = 0; j < resourceKindCount; j++) {
                            allocation1[i][j] = allocation[i][j];
                        }
                        for (int j = 0; j < resourceKindCount; j++) {
                            work1[i][j] = work[i][j];
                        }
                    }
                    System.out.println("Please input the customer’s name and request:");
                    requestName = scanner.next();
                    for (int i = 0; i < resourceKindCount; i++) {
                        request[i] = scanner.nextInt();
                    }
                    for (int i = 0; i < number; i++) {
                        if (requestName.equals(name1[i])) {
                            int c = 0;
                            for (int j = 0; j < resourceKindCount; j++) {
                                if (request[j] <= need1[i][j]) {
                                    c++;
                                }
                            }
                            if (c == resourceKindCount) {
                                int d = 0;
                                for (int j = 0; j < resourceKindCount; j++) {
                                    if (request[j] <= available1[j]) {
                                        d++;
                                    }
                                }
                                if (d == resourceKindCount) {
                                    System.out.println("Name   Work      Need    Allocation   Work+ Allocation  Finish");
                                    for (int j = 0; j < resourceKindCount; j++) {
                                        need1[i][j] = need1[i][j] - request[j];
                                        available1[j] = available1[j] - request[j];
                                        allocation1[i][j] = allocation1[i][j] + request[j];
                                    }
                                    int u = 0;
                                    int o = 0;
                                    while (u != number) {
                                        for (int v = 0; v < number; v++) {
                                            int w = 0;

                                            if (!state1[v].equals("T")) {
                                                for (int jq = 0; jq < resourceKindCount; jq++) {
                                                    if (need1[v][jq] <= available1[jq]) {
                                                        w++;
                                                    }

                                                }
                                            }
                                            if (w == resourceKindCount) {
                                                state1[v] = "T";
                                                for (int p = 0; p < resourceKindCount; p++) {
                                                    work1[v][p] = available1[p];
                                                    add1[v][p] = work1[v][p] + allocation1[v][p];
                                                    available1[p] = add1[v][p];
                                                }
                                                System.out.print(name1[v] + "    ");
                                                for (int A = 0; A < resourceKindCount; A++) {
                                                    System.out.print(work1[v][A] + " ");

                                                }
                                                System.out.print("   ");
                                                for (int y = 0; y < resourceKindCount; y++) {
                                                    System.out.print(need1[v][y] + " ");

                                                }
                                                System.out.print("    ");
                                                for (int z = 0; z < resourceKindCount; z++) {
                                                    System.out.print(allocation1[v][z] + " ");
                                                }
                                                System.out.print("        ");
                                                for (int x1 = 0; x1 < resourceKindCount; x1++) {
                                                    System.out.print(add1[v][x1] + " ");
                                                }
                                                System.out.print("            ");
                                                System.out.println(state1[i]);
                                                u++;
                                                break;
                                            }
                                        }
                                        o++;
                                        if (o > number) {
                                            System.out.println("RESOURCE INSECURITY!!!");
                                            System.out.println("CUSTOMER " + requestName + " CAN NOT  OBTAIN RESOURCES IMMEDIATELY.");
                                            break;
                                        }
                                    }
                                    if (o <= number) {
                                        System.out.println("SYSTEM SECURITY!!!");
                                        System.out.println("CUSTOMER " + requestName + " CAN OBTAIN RESOURCES IMMEDIATELY.");
                                    }

                                } else {
                                    System.out.println("SYSTEM  INSUFFICIENT!!!");
                                    System.out.println("CUSTOMER " + requestName + " CAN NOT  OBTAIN RESOURCES IMMEDIATELY.");
                                }


                            } else {
                                System.out.println("SYSTEM  INSUFFICIENT!!!");
                                System.out.println("CUSTOMER " + requestName + " CAN NOT  OBTAIN RESOURCES IMMEDIATELY.");
                            }
                            h++;
                        }

                    }
                    if (h == 0)
                        System.out.println("no found");
                    break;
                }
                case 3: {
                    return;
                }

            }
        }
    }
}